package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.TeacherShowInfo;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 播放教师风采视频
 */
public class TeacherShowVideoWebActivity extends BaseActivity implements OnClickListener {

    //返回
    private ImageView iv_back;

    private Intent intent;

    private String rowid;

    private String url;

    private WebView mWebView;

    private String title;

    private TextView tv_st_title;

    private RelativeLayout rlLoadingView;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_video_web;
    }

    @Override
    protected void init() {

        showContent();

        initIntent();

        initView();

        initWebView();

        initData();
    }

    /**
     * 接收数据
     */
    private void initIntent() {

        intent = getIntent();
        rowid = intent.getStringExtra("rowid");
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
    }

    /**
     * 初始化webview控件
     */
    private void initWebView() {
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(PluginState.ON);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient());
    }


    /**
     * 初始化控件
     */
    private void initView() {
        rlLoadingView = findViewById(R.id.rl_loading_view);
        iv_back = findViewById(R.id.iv_back);
        mWebView = findViewById(R.id.wb_video);
        tv_st_title = findViewById(R.id.tv_st_title);
        iv_back.setOnClickListener(this);
    }


    /**
     * 初始化数据
     */
    private void initData() {

        //设置标题
        tv_st_title.setText(title);

        mWebView.reload();

//		mWebView.loadUrl("https://v.youku.com/v_show/id_XMzExMzg2NjY0OA==.html?spm=a2h3j.8428770.3416059.1");

//        mWebView.loadUrl("https://v.qq.com/x/cover/5i9nagt2bv6nv6h/o07154ce98v.html");

        mWebView.loadUrl(url);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器

                if (url == null)
                    return false;
                try {
                    if (url.contains("youku") //
                            ) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                                .parse(url));
                        startActivity(intent);
                        return false;
                    }
                } catch (Exception e) { // 防止crash
                    // (如果手机上没有安装处理某个scheme开头的url的APP,
                    // 会导致crash)
                    Log.e("test", e.toString());
                    return true;// 没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
                }
                view.loadUrl(url);
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                return true;
            }
        });


        http();
    }


    /**
     * 上传服务器播放记录
     */
    private void http() {

        Map<String, String> map = new HashMap<>();
        map.put("type", "16");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("rowId", rowid);
        HttpUtils.post(HttpAdress.TEACHERSHOWMINE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {

                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TeacherShowInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TeacherShowInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }
                    Log.i("test", "播放视频数据是：" + response);

                    if (null != info) {
                        if ("200".equals(info.getState())) {


                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.resumeTimers();
        mWebView.onResume();
    }

    @Override
    protected void onDestroy() {
        mWebView.destroy();
        mWebView = null;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();

        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
